package com.jieshuibao.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jieshuibao.common.config.Global;
import com.jieshuibao.model.db.DramaResource;
import com.jieshuibao.model.domain.CookieUser;
import com.jieshuibao.service.DramaResourceService;
import com.jieshuibao.service.RedisService;
import com.jieshuibao.util.StringUtils;
import com.jieshuibao.vedio.po.DramaResourceDto;
import com.jieshuibao.vedio.po.VediosReq;
import com.jieshuibao.vedio.po.VediosRes;
import com.jieshuibao.vedio.po.VerifyCodeReq;
import com.jieshuibao.vedio.po.VerifyCodeRes;

@Controller
@RequestMapping("vedios")
public class DramaResourceController{
	
	@Resource
	private DramaResourceService dramaResourceService;
	
	@Resource
	private RedisService redisService;
	
	@RequestMapping(value="vedios",method=RequestMethod.POST)
	@ResponseBody
	public Object DramaResources(HttpServletRequest request,@RequestBody VediosReq vedioListReq){
		VediosRes res = new VediosRes();
//		String cookieId = request.getHeader("cookieId");
//		if(StringUtils.isEmpty(cookieId)){
//			res.setCode(Global.NOTLOGIN);
//			res.setDes("未登录");
//			return res;
//		}
//		CookieUser cookieUser = redisService.getObejct(cookieId,CookieUser.class);
//		if(null == cookieUser){
//			res.setCode(Global.NOTLOGIN);
//			res.setDes("未登录");
//			return res;
//		}
		if(null == vedioListReq){
			res.setCode(Global.FAIL);
			res.setDes("系统繁忙");
			return res;
		}
		String dramaId = vedioListReq.getDramaId();
		if(StringUtils.isEmpty(dramaId)){
			res.setCode(Global.FAIL);
			res.setDes("系统繁忙");
			return res;
		}
//		String phoneNumber = cookieUser.getPhoneNumber();
		Map<String,Object> map = dramaResourceService.getVedioList(vedioListReq);
		if(null!=map){
			res.setCode(Global.SUCCESS);
			res.setData(map);
			return res;
		}else{
			res.setCode(Global.FAIL);
			res.setDes("系统繁忙");
			return res;
		}
	}
	/**
	 * 获取播放授权码
	 * @param request
	 * @param req
	 * @return
	 */
	@RequestMapping(value="verifyCode",method=RequestMethod.POST)
	@ResponseBody
	public Object getVerifyCode(HttpServletRequest request,@RequestBody VerifyCodeReq req){
		VerifyCodeRes res = new VerifyCodeRes();
		String cookieId = request.getHeader("cookieId");
		if(StringUtils.isEmpty(cookieId)){
			res.setCode(Global.NOTLOGIN);
			res.setDes("未登录");
			return res;
		}
		CookieUser cookieUser = redisService.getObejct(cookieId,CookieUser.class);
		if(null == cookieUser){
			res.setCode(Global.NOTLOGIN);
			res.setDes("未登录");
			return res;
		}
		String phoneNumber = cookieUser.getPhoneNumber();
		if(null == req){
			res.setCode(Global.FAIL);
			res.setDes("系统繁忙");
			return res;
		}
		String verifyCode = dramaResourceService.getVerifyCode(phoneNumber, req);
		Map<String,String> map = new HashMap<String, String>();
		map.put("verifyCode", verifyCode);
		res.setCode(Global.SUCCESS);
		res.setData(map);
		return res;
	}
}
